package com.onemore.task.model;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.voovan.tools.log.Logger;

import com.alibaba.fastjson.JSONObject;
import com.onemore.task.JdbcGlobal;

public class ModelMtab extends JdbcGlobal{
	static public boolean isNotExistTable()
	{
		return validateTableExist("ONE_MTAB");
	}
	static public boolean isExist() throws SQLException
	{
		String sql = "";
		sql = "IF EXISTS (SELECT TOP 1 1 FROM ONE_MTAB) BEGIN SELECT Y=1 END ELSE BEGIN SELECT N=2 END";
		Map<String, Object> map = null;
		map = getJdbcOperate().queryMap(sql);
		return map.containsKey("Y");
	}

    static public void createTable() throws SQLException
    {
    	String sql = "CREATE TABLE [dbo].[ONE_MTAB] ( [KEY_VALUE] varchar(50) COLLATE Chinese_PRC_CI_AS NOT NULL , [ACT_TYPE] varchar(20) COLLATE Chinese_PRC_CI_AS NULL , [TAB_NAME] varchar(20) COLLATE Chinese_PRC_CI_AS NULL , [TIM_STAMP] timestamp NULL )";
    	getJdbcOperate().update(sql);
    }
	static public List<Map<String, Object>> getMtab() throws SQLException
    {
		if(!ModelMtab.isExist()) {
			return null;
		}
		else {}
    	String sql = "";
		sql = "select ACT_TYPE,TAB_NAME,KEY_VALUE,sys.fn_varbintohexstr(TIM_STAMP) as tmstamp from ONE_MTAB";
        List<Map<String, Object>> manyMaps = null;
		manyMaps = getJdbcOperate().queryMapList(sql);

		return manyMaps;
    }

    static public boolean deleteMtab(String stamp) throws SQLException
    {
    	String sql = "";
        sql = "delete from ONE_MTAB where TIM_STAMP="+stamp;
        Map<String, Object> map = null;
        getJdbcOperate().update(sql);
		return true;
    }
}
